package com.cwf.system.controller;

import com.cwf.common.core.controller.BaseController;
import com.cwf.common.entity.http.R;
import com.cwf.common.enums.IsDeleteEnum;
import com.cwf.framework.aspectj.annotation.MyPage;
import com.cwf.system.entity.Dictionary;
import com.cwf.system.service.DictionaryService;
import com.cwf.system.vo.DictionaryVO;
import com.cwf.system.vo.ValidGroup;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@Validated
@RequestMapping("/system/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    DictionaryService dictionaryService;

//
//    /**
//     * 查询所有字典词语并分页
//     * @param dictionary
//     * @return
//     */
//    @MyPage
//    @GetMapping("/page/list")
//    public R<PageInfo<Dictionary>> dictionaryList1(Dictionary dictionary ){
//
//        return R.success(dictionaryService.dictionaryList(dictionary));
//    }

    /**
     * 对搜索内容进行查询并分页
     * @param dictionary
     * @return
     */
    @MyPage
    @GetMapping("/page/list")
    public R searchWord( Dictionary dictionary){

        return R.success(dictionaryService.searchWord(dictionary));
    }

    /**
     * 添加新的字典词语
     * @param dictionary
     * @return
     */
    @PostMapping("/add/word")
    public R addWord(@RequestBody @Validated(value = ValidGroup.Crud.Create.class) Dictionary dictionary){

        DictionaryVO.toVO(dictionary);

        return R.insert(dictionaryService.addWord(dictionary));
    }

    /**
     * 更新字典中词语的信息
     * @param dictionary
     * @return
     */
    @PutMapping("/update/word")
    public R updateWord( @Validated(value = ValidGroup.Crud.Update.class) @RequestBody  Dictionary dictionary){
//isEmpty(dictionaryVO);

     DictionaryVO.toVO(dictionary);
        return R.update(dictionaryService.updateWord(dictionary));
    }

    /**
     * 删除字典中的词语
     * @param id
     * @return
     */

    @DeleteMapping("/delete/{id}")
    public R deleteWord(@PathVariable Integer id ){
        Dictionary build = Dictionary.builder().id(Long.valueOf(id)).isDelete(IsDeleteEnum.OK.getCode()).build();
        return R.delete(dictionaryService.deleteWord(build));
    }


}
